{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/147_chart_array_list.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "**Array and List Charts**\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install -U geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "from geemap import chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_initialize()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scatter plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import the example feature collection and subset the forest feature.\n",
    "forest = ee.FeatureCollection(\"projects/google/charts_feature_example\").filter(\n",
    "    ee.Filter.eq(\"label\", \"Forest\")\n",
    ")\n",
    "\n",
    "# Define a MODIS surface reflectance composite.\n",
    "modisSr = (\n",
    "    ee.ImageCollection(\"MODIS/061/MOD09A1\")\n",
    "    .filter(ee.Filter.date(\"2018-06-01\", \"2018-09-01\"))\n",
    "    .select(\"sur_refl_b0[0-7]\")\n",
    "    .mean()\n",
    ")\n",
    "\n",
    "# Reduce MODIS reflectance bands by forest region; get a dictionary with\n",
    "# band names as keys, pixel values as lists.\n",
    "pixel_vals = modisSr.reduceRegion(\n",
    "    **{\"reducer\": ee.Reducer.toList(), \"geometry\": forest.geometry(), \"scale\": 2000}\n",
    ")\n",
    "\n",
    "# Convert NIR and SWIR value lists to an array to be plotted along the y-axis.\n",
    "y_values = pixel_vals.toArray([\"sur_refl_b02\", \"sur_refl_b06\"])\n",
    "\n",
    "\n",
    "# Get the red band value list; to be plotted along the x-axis.\n",
    "x_values = ee.List(pixel_vals.get(\"sur_refl_b01\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "title = \"Relationship Among Spectral Bands for Forest Pixels\"\n",
    "colors = [\"rgba(29,107,153,0.4)\", \"rgba(207,81,62,0.4)\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.array_values(\n",
    "    y_values,\n",
    "    axis=1,\n",
    "    x_labels=x_values,\n",
    "    series_names=[\"NIR\", \"SWIR\"],\n",
    "    chart_type=\"ScatterChart\",\n",
    "    colors=colors,\n",
    "    title=title,\n",
    "    x_label=\"Red reflectance (x1e4)\",\n",
    "    y_label=\"NIR & SWIR reflectance (x1e4)\",\n",
    "    default_size=15,\n",
    "    xlim=(0, 800),\n",
    ")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/zkPlZIO.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = ee.List(pixel_vals.get(\"sur_refl_b01\"))\n",
    "y = ee.List(pixel_vals.get(\"sur_refl_b06\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.array_values(\n",
    "    y,\n",
    "    x_labels=x,\n",
    "    series_names=[\"SWIR\"],\n",
    "    chart_type=\"ScatterChart\",\n",
    "    colors=[\"rgba(207,81,62,0.4)\"],\n",
    "    title=title,\n",
    "    x_label=\"Red reflectance (x1e4)\",\n",
    "    y_label=\"SWIR reflectance (x1e4)\",\n",
    "    default_size=15,\n",
    "    xlim=(0, 800),\n",
    ")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/WHUHjH6.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " ## Transect line plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define a line across the Olympic Peninsula, USA.\n",
    "transect = ee.Geometry.LineString([[-122.8, 47.8], [-124.5, 47.8]])\n",
    "\n",
    "# Define a pixel coordinate image.\n",
    "lat_lon_img = ee.Image.pixelLonLat()\n",
    "\n",
    "# Import a digital surface model and add latitude and longitude bands.\n",
    "elev_img = ee.Image(\"USGS/SRTMGL1_003\").select(\"elevation\").addBands(lat_lon_img)\n",
    "\n",
    "# Reduce elevation and coordinate bands by transect line; get a dictionary with\n",
    "# band names as keys, pixel values as lists.\n",
    "elev_transect = elev_img.reduceRegion(\n",
    "    reducer=ee.Reducer.toList(),\n",
    "    geometry=transect,\n",
    "    scale=1000,\n",
    ")\n",
    "\n",
    "# Get longitude and elevation value lists from the reduction dictionary.\n",
    "lon = ee.List(elev_transect.get(\"longitude\"))\n",
    "elev = ee.List(elev_transect.get(\"elevation\"))\n",
    "\n",
    "# Sort the longitude and elevation values by ascending longitude.\n",
    "lon_sort = lon.sort(lon)\n",
    "elev_sort = elev.sort(lon)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.array_values(\n",
    "    elev_sort,\n",
    "    x_labels=lon_sort,\n",
    "    series_names=[\"Elevation\"],\n",
    "    chart_type=\"AreaChart\",\n",
    "    colors=[\"#1d6b99\"],\n",
    "    title=\"Elevation Profile Across Longitude\",\n",
    "    x_label=\"Longitude\",\n",
    "    y_label=\"Elevation (m)\",\n",
    "    stroke_width=5,\n",
    "    fill=\"bottom\",\n",
    "    fill_opacities=[0.4],\n",
    "    ylim=(0, 2500),\n",
    ")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/k3XRita.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Metadata scatter plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import a Landsat 8 collection and filter to a single path/row.\n",
    "col = ee.ImageCollection(\"LANDSAT/LC08/C02/T1_L2\").filter(\n",
    "    ee.Filter.expression(\"WRS_PATH ==  45 && WRS_ROW == 30\")\n",
    ")\n",
    "\n",
    "# Reduce image properties to a series of lists; one for each selected property.\n",
    "propVals = col.reduceColumns(\n",
    "    reducer=ee.Reducer.toList().repeat(2),\n",
    "    selectors=[\"CLOUD_COVER\", \"GEOMETRIC_RMSE_MODEL\"],\n",
    ").get(\"list\")\n",
    "\n",
    "# Get selected image property value lists; to be plotted along x and y axes.\n",
    "x = ee.List(ee.List(propVals).get(0))\n",
    "y = ee.List(ee.List(propVals).get(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "colors = [geemap.hex_to_rgba(\"#96356f\", 0.4)]\n",
    "print(colors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.array_values(\n",
    "    y,\n",
    "    x_labels=x,\n",
    "    series_names=[\"RMSE\"],\n",
    "    chart_type=\"ScatterChart\",\n",
    "    colors=colors,\n",
    "    title=\"Landsat 8 Image Collection Metadata (045030)\",\n",
    "    x_label=\"Cloud cover (%)\",\n",
    "    y_label=\"Geometric RMSE (m)\",\n",
    "    default_size=15,\n",
    ")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/3COY3xd.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mapped function scatter & line plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "start = -2 * math.pi\n",
    "end = 2 * math.pi\n",
    "points = ee.List.sequence(start, end, None, 50)\n",
    "\n",
    "\n",
    "def sin_func(val):\n",
    "    return ee.Number(val).sin()\n",
    "\n",
    "\n",
    "values = points.map(sin_func)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.array_values(\n",
    "    values,\n",
    "    points,\n",
    "    chart_type=\"LineChart\",\n",
    "    colors=[\"#39a8a7\"],\n",
    "    title=\"Sine Function\",\n",
    "    x_label=\"radians\",\n",
    "    y_label=\"sin(x)\",\n",
    "    marker=\"circle\",\n",
    ")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/7qcxvey.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "geo",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
